<?php
/**
 * Created by PhpStorm.
 * User: 图图
 * Date: 2021-5-11
 * Time: 9:12
 */
namespace app\common\model;

use think\Db;
use think\Model;

/**
 * 技师汇总报表配置
 * @package app\admin\model
 */
class ReportConfig extends Model
{
    protected $name = 'qt_report_config';
    // 自动写入时间戳
    protected $autoWriteTimestamp = true;

    /**
     * 获取配置
     * @return ReportConfig[]|\think\Collection
     */
    public static function  get_config(){
        $data = self::field('title,name,type')->order('id asc')->all();
        return $data;
    }


    /**
     * 保存配置
     * @param $getData
     * @return bool
     */
    public static function  save_config($getData){
        $check = Db::name('qt_report')->where(['temp_name'=>$getData['temp_name']])->find();
        if(empty($check)){
            $tempDataId = Db::name('qt_report')->insertGetId($getData);
            $configData = json_decode($getData['report_data'],true);
            foreach ($configData as $key=>$value){
                $value['report_id'] = $tempDataId;
                ReportItem::create($value);
            }
            return true;
        }else{
            return true;
        }


    }


    /**
     * 删除配置
     * @param $getData
     * @return bool
     */
    public static function  del_config($getData){
        Db::name('qt_report')->where(['id'=>$getData['report_id']])->delete();
        Db::name('qt_report_item')->where(['report_id'=>$getData['report_id']])->delete();
        return  true;

    }


}
